package com.xyy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xyy.entity.Activity;
import com.xyy.entity.DoctorActivityRegistration;
import com.xyy.entity.DoctorActivityRegistrationVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 医生活动报名Mapper接口
 */
@Mapper
public interface DoctorActivityRegistrationMapper extends BaseMapper<DoctorActivityRegistration> {
    
    /**
     * 获取医生可以报名的活动列表（医生附属管理员发布的活动，且已通过审核）
     * @param doctorId 医生ID
     * @param page 分页对象
     * @return 活动列表
     */
    @Select("SELECT a.* FROM activity a " +
            "JOIN admin ad ON a.admin_id = ad.id " +
            "JOIN doctor d ON d.hospital_name COLLATE utf8mb4_unicode_ci = ad.real_name COLLATE utf8mb4_unicode_ci " +
            "WHERE d.id = #{doctorId} AND d.deleted = 0 AND a.status = 'APPROVED' " +
            "ORDER BY a.created_at DESC")
    IPage<Activity> selectAvailableActivitiesForDoctor(IPage<Activity> page, @Param("doctorId") Long doctorId);
    
    /**
     * 获取医生已报名的活动列表
     * @param doctorId 医生ID
     * @param page 分页对象
     * @return 已报名活动列表
     */
    @Select("SELECT a.*, dar.status as registration_status, dar.registration_time, " +
            "dar.attend_time, dar.complete_time, dar.points_earned " +
            "FROM activity a " +
            "JOIN doctor_activity_registration dar ON a.id = dar.activity_id " +
            "WHERE dar.doctor_id = #{doctorId} " +
            "ORDER BY dar.registration_time DESC")
    IPage<Activity> selectRegisteredActivitiesByDoctor(IPage<Activity> page, @Param("doctorId") Long doctorId);
    
    /**
     * 获取医生已报名的活动列表（包含报名状态信息）
     * @param doctorId 医生ID
     * @param page 分页对象
     * @return 已报名活动列表
     */
    @Select("SELECT a.*, dar.status as registration_status, dar.registration_time, " +
            "dar.attend_time, dar.complete_time, dar.points_earned " +
            "FROM activity a " +
            "JOIN doctor_activity_registration dar ON a.id = dar.activity_id " +
            "WHERE dar.doctor_id = #{doctorId} " +
            "ORDER BY dar.registration_time DESC")
    IPage<com.xyy.vo.DoctorActivityVO> selectRegisteredActivitiesWithStatusByDoctor(IPage<com.xyy.vo.DoctorActivityVO> page, @Param("doctorId") Long doctorId);
    
    /**
     * 统计医生参加过的活动数量
     * @param doctorId 医生ID
     * @return 参加过的活动数量
     */
    @Select("SELECT COUNT(*) FROM doctor_activity_registration " +
            "WHERE doctor_id = #{doctorId} AND status = 'COMPLETED'")
    Integer countAttendedActivitiesByDoctor(@Param("doctorId") Long doctorId);
    
    /**
     * 统计医生总积分
     * @param doctorId 医生ID
     * @return 总积分
     */
    @Select("SELECT COALESCE(SUM(points_earned), 0) FROM doctor_activity_registration " +
            "WHERE doctor_id = #{doctorId}")
    Integer sumPointsByDoctor(@Param("doctorId") Long doctorId);
    
    /**
     * 检查医生是否已报名某个活动
     * @param activityId 活动ID
     * @param doctorId 医生ID
     * @return 报名记录
     */
    @Select("SELECT * FROM doctor_activity_registration " +
            "WHERE activity_id = #{activityId} AND doctor_id = #{doctorId}")
    DoctorActivityRegistration selectByActivityAndDoctor(@Param("activityId") Long activityId, 
                                                         @Param("doctorId") Long doctorId);
    
    /**
     * 获取活动的所有报名医生列表
     * @param activityId 活动ID
     * @param page 分页对象
     * @return 报名医生列表
     */
    @Select("SELECT dar.*, d.real_name as doctor_name, d.hospital_name, d.practice_scope " +
            "FROM doctor_activity_registration dar " +
            "JOIN doctor d ON dar.doctor_id = d.id " +
            "WHERE dar.activity_id = #{activityId} AND d.deleted = 0 " +
            "ORDER BY dar.registration_time DESC")
    IPage<DoctorActivityRegistration> selectRegistrationsByActivity(IPage<DoctorActivityRegistration> page, 
                                                                   @Param("activityId") Long activityId);
    
    /**
     * 获取活动的所有报名医生列表（包含医生详细信息）
     * @param activityId 活动ID
     * @param page 分页对象
     * @return 报名医生列表
     */
    @Select("SELECT dar.*, " +
            "d.real_name as doctor_name, " +
            "d.hospital_name, " +
            "d.practice_scope, " +
            "d.phone, " +
            "d.email, " +
            "d.gender " +
            "FROM doctor_activity_registration dar " +
            "JOIN doctor d ON dar.doctor_id = d.id " +
            "WHERE dar.activity_id = #{activityId} AND d.deleted = 0 " +
            "ORDER BY dar.registration_time DESC")
    IPage<DoctorActivityRegistrationVO> selectRegistrationsByActivityWithDoctorInfo(
        IPage<DoctorActivityRegistrationVO> page, 
        @Param("activityId") Long activityId);
    
    /**
     * 统计医生今日活动数量
     * @param doctorId 医生ID
     * @return 今日活动数量
     */
    @Select("SELECT COUNT(*) FROM doctor_activity_registration dar " +
            "JOIN activity a ON dar.activity_id = a.id " +
            "WHERE dar.doctor_id = #{doctorId} " +
            "AND DATE(a.start_time) = CURDATE() " +
            "AND dar.status = 'COMPLETED'")
    Integer countTodayActivitiesByDoctor(@Param("doctorId") Long doctorId);
    
    /**
     * 统计医生本月活动数量
     * @param doctorId 医生ID
     * @return 本月活动数量
     */
    @Select("SELECT COUNT(*) FROM doctor_activity_registration dar " +
            "JOIN activity a ON dar.activity_id = a.id " +
            "WHERE dar.doctor_id = #{doctorId} " +
            "AND YEAR(a.start_time) = YEAR(CURDATE()) " +
            "AND MONTH(a.start_time) = MONTH(CURDATE()) " +
            "AND dar.status = 'COMPLETED'")
    Integer countThisMonthActivitiesByDoctor(@Param("doctorId") Long doctorId);
    
    /**
     * 统计医生总活动数量
     * @param doctorId 医生ID
     * @return 总活动数量
     */
    @Select("SELECT COUNT(*) FROM doctor_activity_registration " +
            "WHERE doctor_id = #{doctorId} AND status = 'COMPLETED'")
    Integer countTotalActivitiesByDoctor(@Param("doctorId") Long doctorId);
    
    /**
     * 统计本医院全部活动数量
     * @param doctorId 医生ID（用于获取医生所属医院）
     * @return 本医院全部活动数量
     */
    @Select("SELECT COUNT(*) FROM activity a " +
            "JOIN admin ad ON a.admin_id = ad.id " +
            "JOIN doctor d ON d.hospital_name COLLATE utf8mb4_unicode_ci = ad.real_name COLLATE utf8mb4_unicode_ci " +
            "WHERE d.id = #{doctorId}")
    Integer countAllActivitiesByHospital(@Param("doctorId") Long doctorId);
}
